import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:short_drama/models/drama_model.dart';

import '../../../models/home_list_model.dart';
import '../../../utils/http_util.dart';

class HomeListViewmodel extends GetxController {
  String dramaType = '0';
  var dramas = [].obs;
  num pageNum = 1;

  refreshData(List<DramaModel> data) {
    dramas.value = data;
  }

  loadMoreData(List<DramaModel> data) {
    dramas.addAll(data);
  }

  Future<void> queryRefresh() async {
    try {
      final result = await HttpUtil().get(
        'drama/list',
        params: {'drama_type': dramaType, 'page': 1, 'limit': 10},
      );
      final dynamic responseData = result.data['data']; // 明确类型声明
      dramas.value = HomeListModel.fromJson(responseData).data ?? [];
      pageNum++;
    } catch (e) {
      EasyLoading.showToast('请求失败');
    }
  }

  Future<void> queryLoadMore() async {
    try {
      final result = await HttpUtil().get(
        'drama/list',
        params: {'drama_type': dramaType, 'page': pageNum, 'limit': 10},
      );
      final dynamic responseData = result.data['data']; // 明确类型声明
      loadMoreData(HomeListModel.fromJson(responseData).data ?? []);
    } catch (e) {
      EasyLoading.showToast('请求失败');
    }
  }
}
